// pages/member-info/member-info.js
import request from "../../utils/request"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    vips:[],
    phone:'',
    vipTime:'',
    selected: '',
    vipId:'',
    payment:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getVip()
    this.getUser()
  },

  getVip(){
    let that = this
    request({
      url:'/vip/list',
      method:'GET',
    }).then(res=>{
      if(res.code === 200){
        that.setData({
          vips: res.data,
        })
      }else{
        console.log(res.message);
      }
    })
  },
  getUser(){
    let that = this
    request({
      url:'/auth/getUser',
      method:'GET',
    }).then(res=>{
      if(res.code === 200){
        that.setData({
         phone:res.data.phone,
         vipTime:res.data.vip
        })
      }else{
        console.log(res.message);
      }
    })
  },
  vip(){
    let that = this
    if(this.data.payment === "" || this.data.vipId === ""){
      wx.showToast({  
        title: '请选择会员卡',  
        icon: 'error',  
        duration: 2000  
      });  
    }else{
      wx.showModal({
        title: '提示',
        content: '您确定充值吗？',
        success (res) {
          console.log(res);
          if (res.confirm) {
            console.log(that.data);
            request({
              url:'/order/vip',
              method:'POST',
              data:{
                payment: that.data.payment,
                vipId: that.data.vipId 
              }
            }).then(res=>{
              wx.showToast({  
                title: '充值成功',  
                icon: 'success',  
                duration: 2000  
              });  
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }
    
    
  },
  onclick(e){
    this.setData({
      vipId: e.currentTarget.dataset.vip.id,
      selected: e.currentTarget.dataset.vip.id,
      payment: e.currentTarget.dataset.vip.price,
    })
  }
})